package org.dragonnova.meetingapp.app.fragment.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import org.dragonnova.meetingapp.R;
import org.dragonnova.meetingapp.app.activity.HistoryActivity;
import org.dragonnova.meetingapp.app.activity.MeetingGuideActivity;
import org.dragonnova.meetingapp.app.adapter.MeetingAdapter;
import org.dragonnova.meetingapp.model.bean.MeetingInfo;
import org.dragonnova.meetingapp.model.bean.VoiceInfo;
import org.dragonnova.meetingapp.utils.Constants;
import org.dragonnova.meetingapp.utils.ContentResolverUtil;
import org.dragonnova.meetingapp.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * A placeholder fragment containing a simple view.
 */
public class MeetingFragment extends Fragment implements View.OnClickListener {

    private View mContentView;
    private ListView main_meeting_lv;
    private MeetingAdapter mAdapter;
    private List<MeetingInfo> list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.content_meeting, container, false);
        initView();
        return mContentView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    @Override
    public void onResume() {
        super.onResume();
        initHistory();

    }

    public void initView() {
        LinearLayout main_meeting_bt_join = (LinearLayout) mContentView.findViewById(R.id.main_meeting_rl_join);
        LinearLayout main_meeting_bt_create = (LinearLayout) mContentView.findViewById(R.id.main_meeting_rl_create);
        main_meeting_lv = (ListView) mContentView.findViewById(R.id.main_meeting_lv);

        main_meeting_bt_join.setOnClickListener(this);
        main_meeting_bt_create.setOnClickListener(this);

        main_meeting_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MeetingInfo meetingInfo = (MeetingInfo) mAdapter.getItem(position);
                Intent intent = new Intent(getContext(), HistoryActivity.class);
                intent.putExtra("meetingInfo", meetingInfo);
                startActivity(intent);
            }
        });
    }

    public void initData() {
        initHistory();

    }

    public void initHistory() {
        ContentResolverUtil contentResolverUtil = new ContentResolverUtil(getActivity());
        list = contentResolverUtil.findAllMeeting();
        LogUtil.eee("meeting list:" + list.toString());
        for (MeetingInfo info : list) {
            ArrayList<VoiceInfo> voiceInfos = contentResolverUtil.findHistoryById(String.valueOf(info.getSerial()));
            LogUtil.eee("voicedata list:" + voiceInfos.toString());
        }
        //初始化聊天记录
        mAdapter = new MeetingAdapter(getContext(), list);
        main_meeting_lv.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        Intent intent;
        switch (viewId) {
            case R.id.main_meeting_rl_create:
                intent = new Intent(getActivity(), MeetingGuideActivity.class);
                intent.putExtra(Constants.ACTIVITY_INTENT_ACTION, Constants.ACTIVITY_ACTION_CREATOR);
                startActivity(intent);
                break;
            case R.id.main_meeting_rl_join:
                intent = new Intent(getActivity(), MeetingGuideActivity.class);
                intent.putExtra(Constants.ACTIVITY_INTENT_ACTION, Constants.ACTIVITY_ACTION_PARTICIPANT);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
